﻿using System;
using System.Diagnostics;
using System.Windows;
using Caliburn.Micro;
using Microsoft.Phone.Shell;
using ScreenRotateSample.Messages;

namespace ScreenRotateSample.ViewModels
{
    public class Page1ViewModel : Screen, IHandle<SampleMessage>
    {
        private readonly IEventAggregator eventAggregator;
        private readonly INavigationService navigationService;

        public Page1ViewModel(INavigationService navigationService, IEventAggregator eventAggregator)
        {
            this.navigationService = navigationService;
            this.eventAggregator = eventAggregator;

            eventAggregator.Subscribe(this);

            Name = "Binding Automático";

            DisplayName = "Page1";
        }

        public void Handle(SampleMessage message)
        {
            Name = message.Name;
        }

        #region Property Example
        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                NotifyOfPropertyChange(() => Name);
            }
        }
        #endregion

        #region Enable/Disable Command
        private bool isEnabled;

        public bool IsEnabled
        {
            get { return isEnabled; }
            set
            {
                isEnabled = value;
                NotifyOfPropertyChange(() => IsEnabled);
                NotifyOfPropertyChange(() => CanShowName);
            }
        }

        public bool CanShowName
        {
            get { return IsEnabled; }
        }
        #endregion

        #region Command
        public void ShowName() 
        {
            MessageBox.Show(Name);
            navigationService.UriFor<DetailsViewModel>().Navigate();
        }
        #endregion

        public void CreateTile()
        {
            ShellTileData tile = new StandardTileData
            {
                Title = "Test",
            };

            ShellTile.Create(new Uri("/Views/MainView.xaml?Name=Tile", UriKind.Relative), tile);
        }


        #region ViewEvents
        protected override void OnViewAttached(object view, object context)
        {
            base.OnViewAttached(view, context);
            Debug.WriteLine("OnViewAttached");
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
            Debug.WriteLine("OnInitialize");
        }

        protected override void OnViewReady(object view)
        {
            base.OnViewReady(view);
            Debug.WriteLine("OnViewReady");
        }

        protected override void OnActivate()
        {
            base.OnActivate();
            Debug.WriteLine("OnActivate:");
        }

        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);
            Debug.WriteLine("OnViewLoaded");
        }

        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);
            Debug.WriteLine("OnDeactivate");
        }
        #endregion
    }
}
